/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.commons.result;

import org.ipig.constants.ResultStatus;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;

/**
 * 通用 result
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: GenericResult.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@Data
@ToString
public class GenericResult<T> extends Result {
    /**
     * 状态
     */
    private String status;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 附件对象
     */
    private T payload;

    /**
     * struct
     */
    public GenericResult() {
        this.setMessage(ResultStatus.SUCCESS.getTitle());
        this.setStatus(ResultStatus.SUCCESS);
        this.setPayload(payload);
    }

    /**
     * @param payload 附件对象
     */
    public GenericResult(T payload) {
        this.setMessage(ResultStatus.SUCCESS.getTitle());
        this.setStatus(ResultStatus.SUCCESS);
        this.setPayload(payload);
    }

    /**
     * @param resultState ResultState
     * @param message     消息
     * @param payload     附件对象
     */
    public GenericResult(ResultStatus resultState, String message, T payload) {
        this.setMessage(message);
        this.setStatus(resultState);
        this.setPayload(payload);
    }

    /**
     * @param resultStatus ResultStatus
     * @param payload      附件对象
     */
    public GenericResult(ResultStatus resultStatus, T payload) {
        this.setMessage(resultStatus.getTitle());
        this.setStatus(resultStatus);
        this.setPayload(payload);
    }

    /**
     * @param resultState ResultState
     */
    public GenericResult(ResultStatus resultState) {
        this.setMessage(resultState.getTitle());
        this.setStatus(resultState);
    }

    /**
     * @param resultStatus ResultStatus
     * @param msg          消息
     */
    public GenericResult(ResultStatus resultStatus, String msg) {
        this.setMessage(msg);
        this.setStatus(resultStatus);
    }


    /**
     * setStatus
     *
     * @param resultStatus
     */
    public void setStatus(ResultStatus resultStatus) {
        this.status = resultStatus.getValue();
        if (StringUtils.isBlank(this.message)) {
            this.message = resultStatus.getTitle();
        }
    }

    /**
     * setStatus
     *
     * @param status String
     */
    public void setStatus(String status) {
        this.status = status;
    }
}
